package com.xidian.bankdemo.controller;

import com.xidian.bankdemo.common.Result;
import com.xidian.bankdemo.dto.LoginInfoDTO;
import com.xidian.bankdemo.entity.Account;
import com.xidian.bankdemo.entity.User;
import com.xidian.bankdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/hello")
    public String sayHello(){
        return "hello";
    }
    @GetMapping("/hello2")
    public String sayHelloAgain(){
        return "hello2";
    }

    @PostMapping("/login")
    public Result doLogin(@RequestBody LoginInfoDTO user) {
        String jwtToken = userService.doLogin(user.getUsername(), user.getPassword());// 处理密码
        if (jwtToken == null) {
            return Result.ERROR("用户名或密码错误!");
        }
        return Result.OK(jwtToken);
    }
    @PostMapping({"/register", "/admin/new"})
    public Result doRegister(@RequestBody User user) {
        switch (userService.doRegister(user)) {
            case 0: return Result.ERROR("注册失败！");
            case 1: return Result.OK();
            case 2: return Result.ERROR("用户已存在！");
            default: return Result.ERROR("服务器内部错误！");
        }
    }
    @GetMapping("/info")
    public Result getInfo(@RequestParam("token") String token) {
        User info = userService.getInfo(token);
        if (info == null) {
            return Result.ERROR("未找到用户!");
        }
        return Result.OK(info);
    }
    @PostMapping("/info")
    public Result updateInfo(@RequestBody User userInfo) {
        if (1 == userService.setInfo(userInfo)) return Result.OK();
        return Result.ERROR("修改失败!");
    }
    @PostMapping("/pwd")
    public Result updatePwd(@RequestBody LoginInfoDTO user) {
        if (1 == userService.setPwd(user)) return Result.OK();
        return Result.ERROR("修改失败!");
    }

    @GetMapping("/code")
    public Result genCode(@RequestParam("id") Long id) {
        int code = userService.genCode(id);
        if (code == 0) {
            return Result.ERROR("服务器内部错误，生成验证码失败！");
        }
        return Result.OK(code); // TODO:实际使用短信发送验证码
    }
    @GetMapping("/verify/code")
    public Result verifyCode(@RequestParam("name") String name, @RequestParam("verifyCode") int verifyCode) {
        return Result.OK(userService.verifyCode(name, verifyCode));
    }
    @PostMapping("/verify/pwd")
    public Result verifyPwd(@RequestBody LoginInfoDTO user) {
        return Result.OK(userService.verifyPwd(user));
    }
    @PostMapping("/logout")
    public Result doLogout() {
        return Result.OK();
    }
    @GetMapping("/admin/list")
    public Result getList(String token){
        List<User> userList = userService.getList(token);
        return Result.OK(userList);
    }
    @PostMapping("/admin/edit")
    public Result editUser(@RequestBody User user) {
        if (userService.editUser(user) == 1) {
            return Result.OK();
        }
        return Result.ERROR("修改用户失败");
    }
    @DeleteMapping("/admin/del")
    public Result delUser(@RequestParam("id") Long id) {
        if (userService.delUser(id) == 1) {
            return Result.OK();
        }
        return Result.ERROR("删除用户失败");
    }
}
